<?php
/**
 *$now_time = time();
 *$date=date("Y-m-d",$now_time);
 *$day1   = get_date($date,'d');
 *$day2   = get_date($date,'d',-1);
 *$week1  = get_date($date,'w');
 *$week2  = get_date($date,'w',-1);
 *$month1 = get_date($date,'m');
 *$month2 = get_date($date,'m',-1);
 */
if ( ! function_exists('force_download'))
{
	function date_span($date,$t='d',$n=0)
	{
		if($t=='d'){
			$firstday = date('Y-m-d 00:00:00',strtotime("$n day"));
			$lastday = date("Y-m-d 23:59:59",strtotime("$n day"));

		}elseif($t=='w'){
			if($n!=0){$date =  date('Y-m-d',strtotime("$n week"));}
			$lastday = date("Y-m-d 23:59:59",strtotime("$date Sunday"));
			$firstday = date("Y-m-d 00:00:00",strtotime("$lastday -6 days"));

		}elseif($t=='m'){
			if($n!=0){$date = date('Y-m-d',strtotime("$n months"));}
			$firstday = date("Y-m-01 00:00:00",strtotime($date));
			$lastday = date("Y-m-d 23:59:59",strtotime("$firstday +1 month -1 day"));	
		}
		return array($firstday,$lastday);
	}
}

/**
 * Number of days in a month
 *
 * Takes a month/year as input and returns the number of days
 * for the given month/year. Takes leap years into consideration.
 *
 * @access	public
 * @param	integer a numeric month
 * @param	integer	a numeric year
 * @return	integer
 */	
if ( ! function_exists('days_in_month'))
{
	function days_in_month($month = 0, $year = '')
	{
		if ($month < 1 OR $month > 12)
		{
			return 0;
		}
	
		if ( ! is_numeric($year) OR strlen($year) != 4)
		{
			$year = date('Y');
		}
	
		if ($month == 2)
		{
			if ($year % 400 == 0 OR ($year % 4 == 0 AND $year % 100 != 0))
			{
				return 29;
			}
		}

		$days_in_month	= array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		return $days_in_month[$month - 1];
	}
}

// ------------------------------------------------------------------------

/**
 * Converts a local Unix timestamp to GMT
 *
 * @access	public
 * @param	integer Unix timestamp
 * @return	integer
 */	
if ( ! function_exists('local_to_gmt'))
{
	function local_to_gmt($time = '')
	{
		if ($time == '')
			$time = time();
	
		return mktime( gmdate("H", $time), gmdate("i", $time), gmdate("s", $time), gmdate("m", $time), gmdate("d", $time), gmdate("Y", $time));
	}
}
	
// ------------------------------------------------------------------------

/**
 * Converts GMT time to a localized value
 *
 * Takes a Unix timestamp (in GMT) as input, and returns
 * at the local value based on the timezone and DST setting
 * submitted
 *
 * @access	public
 * @param	integer Unix timestamp
 * @param	string	timezone
 * @param	bool	whether DST is active
 * @return	integer
 */	
if ( ! function_exists('gmt_to_local'))
{
	function gmt_to_local($time = '', $timezone = 'UTC', $dst = FALSE)
	{			
		if ($time == '')
		{
			return now();
		}
	
		$time += timezones($timezone) * 3600;

		if ($dst == TRUE)
		{
			$time += 3600;
		}
	
		return $time;
	}
}
	
// ------------------------------------------------------------------------

/**
 * Converts a MySQL Timestamp to Unix
 *
 * @access	public
 * @param	integer Unix timestamp
 * @return	integer
 */	
if ( ! function_exists('mysql_to_unix'))
{
	function mysql_to_unix($time = '')
	{
		// We'll remove certain characters for backward compatibility
		// since the formatting changed with MySQL 4.1
		// YYYY-MM-DD HH:MM:SS
	
		$time = str_replace('-', '', $time);
		$time = str_replace(':', '', $time);
		$time = str_replace(' ', '', $time);
	
		// YYYYMMDDHHMMSS
		return  mktime(
						substr($time, 8, 2),
						substr($time, 10, 2),
						substr($time, 12, 2),
						substr($time, 4, 2),
						substr($time, 6, 2),
						substr($time, 0, 4)
						);
	}
}

// ------------------------------------------------------------------------

/**
 * Timezone Menu
 *
 * Generates a drop-down menu of timezones.
 *
 * @access	public
 * @param	string	timezone
 * @param	string	classname
 * @param	string	menu name
 * @return	string
 */	
if ( ! function_exists('timezone_menu'))
{
	function timezone_menu($default = 'UTC', $class = "", $name = 'timezones')
	{
		$lang['UM12']	= '(UTC -12:00) Baker/Howland Island';
		$lang['UM11']	= '(UTC -11:00) Samoa Time Zone, Niue';
		$lang['UM10']	= '(UTC -10:00) Hawaii-Aleutian Standard Time, Cook Islands, Tahiti';
		$lang['UM95']	= '(UTC -9:30) Marquesas Islands';
		$lang['UM9']	= '(UTC -9:00) Alaska Standard Time, Gambier Islands';
		$lang['UM8']	= '(UTC -8:00) Pacific Standard Time, Clipperton Island';
		$lang['UM7']	= '(UTC -7:00) Mountain Standard Time';
		$lang['UM6']	= '(UTC -6:00) Central Standard Time';
		$lang['UM5']	= '(UTC -5:00) Eastern Standard Time, Western Caribbean Standard Time';
		$lang['UM45']	= '(UTC -4:30) Venezuelan Standard Time';
		$lang['UM4']	= '(UTC -4:00) Atlantic Standard Time, Eastern Caribbean Standard Time';
		$lang['UM35']	= '(UTC -3:30) Newfoundland Standard Time';
		$lang['UM3']	= '(UTC -3:00) Argentina, Brazil, French Guiana, Uruguay';
		$lang['UM2']	= '(UTC -2:00) South Georgia/South Sandwich Islands';
		$lang['UM1']	= '(UTC -1:00) Azores, Cape Verde Islands';
		$lang['UTC']	= '(UTC) Greenwich Mean Time, Western European Time';
		$lang['UP1']	= '(UTC +1:00) Central European Time, West Africa Time';
		$lang['UP2']	= '(UTC +2:00) Central Africa Time, Eastern European Time, Kaliningrad Time';
		$lang['UP3']	= '(UTC +3:00) Moscow Time, East Africa Time';
		$lang['UP35']	= '(UTC +3:30) Iran Standard Time';
		$lang['UP4']	= '(UTC +4:00) Azerbaijan Standard Time, Samara Time';
		$lang['UP45']	= '(UTC +4:30) Afghanistan';
		$lang['UP5']	= '(UTC +5:00) Pakistan Standard Time, Yekaterinburg Time';
		$lang['UP55']	= '(UTC +5:30) Indian Standard Time, Sri Lanka Time';
		$lang['UP575']	= '(UTC +5:45) Nepal Time';
		$lang['UP6']	= '(UTC +6:00) Bangladesh Standard Time, Bhutan Time, Omsk Time';
		$lang['UP65']	= '(UTC +6:30) Cocos Islands, Myanmar';
		$lang['UP7']	= '(UTC +7:00) Krasnoyarsk Time, Cambodia, Laos, Thailand, Vietnam';
		$lang['UP8']	= '(UTC +8:00) Australian Western Standard Time, Beijing Time, Irkutsk Time';
		$lang['UP875']	= '(UTC +8:45) Australian Central Western Standard Time';
		$lang['UP9']	= '(UTC +9:00) Japan Standard Time, Korea Standard Time, Yakutsk Time';
		$lang['UP95']	= '(UTC +9:30) Australian Central Standard Time';
		$lang['UP10']	= '(UTC +10:00) Australian Eastern Standard Time, Vladivostok Time';
		$lang['UP105']	= '(UTC +10:30) Lord Howe Island';
		$lang['UP11']	= '(UTC +11:00) Magadan Time, Solomon Islands, Vanuatu';
		$lang['UP115']	= '(UTC +11:30) Norfolk Island';
		$lang['UP12']	= '(UTC +12:00) Fiji, Gilbert Islands, Kamchatka Time, New Zealand Standard Time';
		$lang['UP1275']	= '(UTC +12:45) Chatham Islands Standard Time';
		$lang['UP13']	= '(UTC +13:00) Phoenix Islands Time, Tonga';
		$lang['UP14']	= '(UTC +14:00) Line Islands';
	
		if ($default == 'GMT')
			$default = 'UTC';

		$menu = '<select name="'.$name.'"';
	
		if ($class != '')
		{
			$menu .= ' class="'.$class.'"';
		}
	
		$menu .= ">\n";
	
		foreach (timezones() as $key => $val)
		{
			$selected = ($default == $key) ? " selected='selected'" : '';
			$menu .= "<option value='{$key}'{$selected}>".$lang[$key]."</option>\n";
		}

		$menu .= "</select>";

		return $menu;
	}
}
	
// ------------------------------------------------------------------------

/**
 * Timezones
 *
 * Returns an array of timezones.  This is a helper function
 * for various other ones in this library
 *
 * @access	public
 * @param	string	timezone
 * @return	string
 */	
if ( ! function_exists('timezones'))
{
	function timezones($tz = '')
	{
		// Note: Don't change the order of these even though
		// some items appear to be in the wrong order
		
		$zones = array( 
						'UM12'		=> -12,
						'UM11'		=> -11,
						'UM10'		=> -10,
						'UM95'		=> -9.5,
						'UM9'		=> -9,
						'UM8'		=> -8,
						'UM7'		=> -7,
						'UM6'		=> -6,
						'UM5'		=> -5,
						'UM45'		=> -4.5,
						'UM4'		=> -4,
						'UM35'		=> -3.5,
						'UM3'		=> -3,
						'UM2'		=> -2,
						'UM1'		=> -1,
						'UTC'		=> 0,
						'UP1'		=> +1,
						'UP2'		=> +2,
						'UP3'		=> +3,
						'UP35'		=> +3.5,
						'UP4'		=> +4,
						'UP45'		=> +4.5,
						'UP5'		=> +5,
						'UP55'		=> +5.5,
						'UP575'		=> +5.75,
						'UP6'		=> +6,
						'UP65'		=> +6.5,
						'UP7'		=> +7,
						'UP8'		=> +8,
						'UP875'		=> +8.75,
						'UP9'		=> +9,
						'UP95'		=> +9.5,
						'UP10'		=> +10,
						'UP105'		=> +10.5,
						'UP11'		=> +11,
						'UP115'		=> +11.5,
						'UP12'		=> +12,
						'UP1275'	=> +12.75,
						'UP13'		=> +13,
						'UP14'		=> +14
					);
				
		if ($tz == '')
		{
			return $zones;
		}
	
		if ($tz == 'GMT')
			$tz = 'UTC';
	
		return ( ! isset($zones[$tz])) ? 0 : $zones[$tz];
	}
}